<?php
/**
 * Created by PhpStorm.
 * User: wuxiang
 * Date: 2016/7/20
 * Time: 14:28
 * 运营后台秒杀活动
 */

namespace Grab\Controller;

use Common\Controller\AdminbaseController;
use Common\Libiary\BaseApiHttp;
use Common\Libiary\Uploadify;

class GrabController extends AdminbaseController
{
    public $url = '';
    public $data = [];
    public $arr = [];
    public $res = [];
    public $arrres = [];
    public $page = 1;
    public $type;
    public $list;
    public $demain = TP_DEBUG ?  "http://m.9now.net/" : "http://m.mwee.cn/";

    function index(){
        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();

        $this->url = $this->demain . 'api/operate/grab/index';

        $this->page = I('get.Page',1,'intval');

        $this->data['Page'] = $this->page;

        $ret = $baseApi->https($this->url, 'POST', $this->data);

        $this->list = json_decode($ret, true);

       // print_r($this->list);

        $this->assign('list',$this->list);


        $this->display();
    }

    //获取更多
    function getMore(){
        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();

        $this->url = $this->demain . 'api/operate/grab/index';

        $this->page = I('Post.Page',1,'intval');

        $this->data['Page'] = $this->page;

        $ret = $baseApi->https($this->url, 'POST', $this->data);

       // print_r($ret);
        //exit;
        $this->list = json_decode($ret, true);

        $this->ajaxReturn($this->list);
    }

    function add(){
        $this->display();
    }

    //添加处理
    function addGarbHandle(){

        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();
        $this->url = $this->demain . 'api/operate/grab/addhandel';
        //获取数据
        $this->res = $this->getData();

        $this->res['ADMIN_ID'] = $_SESSION['ADMIN_ID'];
        $this->res['ADMIN_USERNAME'] = $_SESSION['ADMIN_USERNAME'];
        $this->res['LAST_LOGIN_TIME'] = $_SESSION['LAST_LOGIN_TIME'];

        $list = [];

        //组合时间和日期
        foreach($this->res['citylist'] as $k=>$v){
            $list['city'][$v]['date'] = $this->res['dateRange'];
            $list['city'][$v]['time'] = $this->res['timeRange'];

        }

        $this->data = array_merge($this->data,$list,$this->res);

        $ret = $baseApi->https($this->url, 'POST', $this->data);

        $this->list = json_decode($ret, true);


        $this->ajaxReturn($this->list);

    }

    //查看

    function view(){
        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();
        $this->url = $this->demain . 'api/operate/grab/grabview';

        $this->data['ActivityID'] = I('get.ActivityID',1,'intval');
        $ret = $baseApi->https($this->url, 'POST', $this->data);

        $this->list = json_decode($ret, true);

        foreach($this->list['data']['city'] as $item=>$row){
            $this->list['data']['city'][$item]['coupon'] = json_decode($row['AmountList'],true);
        }

        $this->assign('ActivityID',$this->data['ActivityID']);
        $this->assign('list',$this->list);

        $this->display('actview');
    }



    //编辑活动
    function editGrab(){
        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();
        $this->url = $this->demain . 'api/operate/grab/editgarb';
        $this->data['ActivityID'] = I('get.ActivityID','1','intval');
        $ret = $baseApi->https($this->url, 'POST', $this->data);

        $this->list = json_decode($ret, true);
       // print_r($this->list);

        $this->assign('list',$this->list);
        $this->display('edit');
    }

    /**
     * 上传成功返回id和url
     * 此功能是为了复用以前的图片
     */
    public function uploadify()
    {

        $uploadType = I('post.uplaodtype','');

        $upload = new Uploadify();
        $arr = $upload->uploadify($_FILES['coverImg']);

        if($uploadType){
            $arr = $this->editPicSetSql($arr);
        }

       echo  json_encode($arr);
    }




    /*
     * 图片修改入库
     */
    function editPicSetSql($arr){

        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();
        $this->url = $this->demain . 'api/operate/grab/editpicsetsqlapi';
        $this->data['Pic'] = $arr;
        $ret = $baseApi->https($this->url, 'POST', $this->data);
        $this->list = json_decode($ret, true);
        return $this->list;
    }

    //编辑处理
    function editGarbHandle(){

        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();
        $this->url = $this->demain . 'api/operate/grab/edithandel';
        //获取数据

        $this->res['ActivityID'] = I('post.ActivityID',1,'intval');
        $this->res['Name'] = I('post.Name');
        $this->res['Desc'] = I('post.Desc');
        $this->res['PrizeLimit'] = I('post.prizeLimit',1,'intval');

        $this->res['ADMIN_ID'] = $_SESSION['ADMIN_ID'];
        $this->res['ADMIN_USERNAME'] = $_SESSION['ADMIN_USERNAME'];
        $this->res['LAST_LOGIN_TIME'] = $_SESSION['LAST_LOGIN_TIME'];

        $this->data = array_merge($this->data,$this->res);
        $ret = $baseApi->https($this->url, 'POST', $this->data);

        $this->list = json_decode($ret, true);

        $this->ajaxReturn($this->list);

    }


    //获取提交的数据
    private function getData(){
        $this->arrres['Name'] = $_POST['Name'];
        $this->arrres['Pic'] = $_POST['Pic'];
        $this->arrres['Desc'] = $_POST['Desc'];
        $this->arrres['PrizeLimit'] = $_POST['prizeLimit'];
        $this->arrres['timeRange'] = json_decode($_POST['timeRange'],true);
        $this->arrres['dateRange'] = isset($_POST['dateRange'])?json_decode($_POST['dateRange'],true):'';
        $this->arrres['citylist'] = json_decode($_POST['citylist'],true);
        $this->arrres['timeid'] =isset($_POST['timeid']) ?  $_POST['timeid'] : '';
        return $this->arrres;
    }




    //添加场次
    function addHandle(){

        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();
        $this->url = $this->demain . 'api/operate/grab/addplayhandel';

        $this->data['ActivityID'] = I('post.ActivityID',1,'intval');
        $this->data['AttributeID'] = I('post.AttributeID',1,'intval');
        $this->data['type'] = I('post.type',1,'intval');

        $this->data['ADMIN_ID'] = $_SESSION['ADMIN_ID'];
        $this->data['ADMIN_USERNAME'] = $_SESSION['ADMIN_USERNAME'];
        $this->data['LAST_LOGIN_TIME'] = $_SESSION['LAST_LOGIN_TIME'];

        $this->arr['cityID'] = $_POST['cityID'];

        $this->arr['prizeName'] = $_POST['prizename'];
        $this->arr['giftid'] = $_POST['giftid'];


        $this->arr['icon'] = $_POST['icon'];
        $this->arr['shopCoupon'] = $_POST['shopCoupon'];

        $this->arr['field'] = $_POST['field'];
        $this->arr['intro'] = $_POST['intro'];
        $this->arr['gift'] = json_decode($_POST['gift'],true);
        $this->arr['timeID'] = array_unique($_POST['timeID']);


        //组合奖品
        foreach($this->arr['prizeName'] as $k=>$v){
            $this->res[$k]['prizeName'] = $v;
            $this->res[$k]['icon'] = $this->arr['icon'][$k];
            $this->res[$k]['giftid'] = $this->arr['giftid'][$k];
            $this->res[$k]['shopid'] = $this->getArrayFirstValue($this->checkMount($this->arr['shopCoupon'][$k],2));
            $this->res[$k]['field'] = $this->arr['field'][$k];
            $this->res[$k]['intro'] = $this->arr['intro'][$k];
        }

        //组合场次-------
        $gift = $this->arr['gift'];
        $giftList = [];
        foreach($gift as $item=>$row){
           $giftList[$item] = array_filter($row);
        }

        $date = [];
        $times = [];

        foreach($giftList as $key=>$val){
            $date[] =$key;
            foreach($val as $_key=>$_val){
                $time[$_key] = $_val;
            }
        }

        foreach($time as $k=>$v){
            $times[] = $k;
        }

        $list = $this->data;
        $list['arr'][$this->arr['cityID']] = ['timeRange'=>$times]+['dateRange'=>$date]+['gift'=>$this->res]+['play'=>$giftList];

       // print_r($list);
        //exit;
        $ret = $baseApi->https($this->url, 'POST', $list);

        $listInfo = json_decode($ret, true);
        $this->ajaxReturn($listInfo);

    }

    //查出分店名称
    function getBranchName()
    {
        //返回主店ID
        $branch = [];
        $baseApi = new BaseApiHttp();
        $branch['CouponID'] =I('post.couponID','','trim');
        $this->data = $baseApi->build_sparamsyy($branch);
        $this->url='http://m.mwee.cn/api/coupons/info';
        if(TP_DEBUG)  $this->url='http://m.9now.net/api/coupons/info';
        $ret = $baseApi->https($this->url, 'POST', $this->data);
        $this->list = json_decode($ret, true);
        return $this->ajaxReturn($this->list['data']['general_info']);
    }

    /**
     * 通过分店id获取分店信息
     */
    function getBrachInfo(){

        //返回主店ID
          $baseApi = new BaseApiHttp();
          $this->res['CouponID'] = I('post.branchid','','trim');
          $this->data = $baseApi->build_sparamsyy($this->res);
          $this->url='http://m.mwee.cn/api/coupons/info';
          if(TP_DEBUG) $this->url='http://m.9now.net/api/coupons/info';
          $ret = $baseApi->https($this->url, 'POST', $this->data);
          $this->list = json_decode($ret, true);

       // 返回店铺列表ID
        if(isset($this->list['data'])){
            $this->arr['CouponID'] = $this->res['CouponID'];
            $this->arr['MasterID'] = $this->list['data']['general_info']['m_shopid'];
            $this->arrres = $baseApi->build_sparamsyy($this->arr);

            $urlInfo='http://m.mwee.cn/api/coupons/branchlist';
            if(TP_DEBUG) $urlInfo='http://m.9now.net/api/coupons/branchlist';

            $http = $baseApi->https($urlInfo, 'POST', $this->arrres);
            $result = json_decode($http, true);

            //通过店铺id列表查店铺名称列表

            $shopBranch = [];
            if(count($result['data'])>0){
                $shopBranch['shopid'] = implode(',',$result['data']);
                $shopBranch['entitystore'] = 2;
            }

            $branchurl='http://10.0.26.9/searchlistv2';
            if(TP_DEBUG) $branchurl='http://es.9now.net/searchlistv2';

            $shopret = $baseApi->https($branchurl, 'POST',$shopBranch);

            $resultRes = json_decode($shopret, true);
            $resultRes['couponName'] = $this->list['data']['general_info']['coupon_name'];
            return $this->ajaxReturn($resultRes);

        }

    }




    //添加秒杀活动
    function addGrab(){

        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();

        $this->data['ActivityID'] =   I('get.ActivityID',1,'intval');
        $this->data['AttributeID'] = I('get.AttributeID',1,'intval');
        $this->data['CityID'] = I('get.CityID',258,'intval');

        $this->url = $this->demain . 'api/operate/grab/getcitytimes';

        //print_r($this->data);
        $ret = $baseApi->https($this->url, 'POST', $this->data);

        $this->list = json_decode($ret, true);

        $this->assign('ActivityID',$this->data['ActivityID']);
        $this->assign('AttributeID',$this->data['AttributeID']);
        $this->assign('CityID',$this->data['CityID']);



        if(isset($this->list['data']['timesRange']['gift'])){
            foreach($this->list['data']['timesRange']['gift'] as $k=>$v){
                $this->list['data']['timesRange']['gift'][$k]['shopName'] = $this->getBranchList(implode(',',$v['ShopBranchList']));
            }
        }


        $this->assign('list',$this->list);


        $this->display('activity');
    }

    /**
     * 获取参与人数数据
     */
    function getJoinList(){

        $baseApi = new BaseApiHttp();
        $ActivityID = I('ActivityID','1','intval');
        $this->url = $this->demain . 'api/operate/grab/getjoin';
        //获取数据
        $this->res['ActivityID'] = $ActivityID;
        $ret = $baseApi->https($this->url, 'POST', $this->res);

        print_r($ret);
        //$this->list = json_decode($ret, true);

        $this->display('join');
    }

    function getBranchList($shopBranchID){
        $baseApi = new BaseApiHttp();
        $this->data = $baseApi->build_sparams();
        $shopBranch = [];
        $shopBranch['shopid'] = $shopBranchID;
        $shopBranch['entitystore'] = 2;

        $branchurl='http://10.0.26.9/searchlistv2';
        if(TP_DEBUG)$branchurl='http://es.9now.net/searchlistv2';
        $shopret = $baseApi->https($branchurl, 'POST',$shopBranch);
        $resultRes = json_decode($shopret, true);

        return isset($resultRes['data']['list']) ? $resultRes['data']['list'] : [];
    }

    //整合库存
    function checkMount($data,$count){

        return array_chunk($data,$count);

    }

    function getArrayFirstValue($data){
        $list = [];
        if(!is_array($data)){
            return false;
        }

        foreach($data as $k=>$v){
            $list[] = $v[0];
        }
        return $list;
    }

    //整合奖品详情
    private function CheckPrizeField($data){
        $datainfo = [];
       if($data){
           $data = array_chunk($data,2);
           foreach($data as $k=>$v){
               $datainfo[$v[0]] = $v[1];
           }
           return $datainfo;
       }
    }

    //测试日志写入
    function writelog(){
        $baseApi = new BaseApiHttp();
        $this->data = [
            'time' => date('Y-m-d H:i:s'),
            'data' => 'hello world',
        ];
        $this->url = $this->demain . 'api/operate/grab/test';
        $ret = $baseApi->https($this->url, 'POST',$this->data);
        print_r($ret);

        exit;
    }

}

